package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ManageServiceImpl implements ManageService {


    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;
    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;
    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;
    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;


    @Override
    public List<BaseCategory1> getCategory1() {
        return baseCategory1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategory2(Long category1Id) {
        QueryWrapper<BaseCategory2> baseCategory2QueryWrapper = new QueryWrapper<>();
        baseCategory2QueryWrapper.eq("category1_id", category1Id);
        return baseCategory2Mapper.selectList(baseCategory2QueryWrapper);

    }

    @Override
    public List<BaseCategory3> getCategory3(Long category2Id) {
        QueryWrapper<BaseCategory3> baseCategory3QueryWrapper = new QueryWrapper<>();
        baseCategory3QueryWrapper.eq("category2_id", category2Id);
        return baseCategory3Mapper.selectList(baseCategory3QueryWrapper);
    }

    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.getAttrInfoList(category1Id, category2Id, category3Id);
        return baseAttrInfoList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        QueryWrapper<BaseAttrInfo> baseAttrInfoQueryWrapper = new QueryWrapper<>();
        baseAttrInfoQueryWrapper.eq("attr_name", baseAttrInfo.getAttrName());
        baseAttrInfoQueryWrapper.eq("category_id", baseAttrInfo.getCategoryId());
        baseAttrInfoQueryWrapper.eq("category_level", baseAttrInfo.getCategoryLevel());
        BaseAttrInfo baseAttrInfo1 = baseAttrInfoMapper.selectOne(baseAttrInfoQueryWrapper);
        if (null == baseAttrInfo1) {
            baseAttrInfoMapper.saveAttrInfo(baseAttrInfo);
            if (!CollectionUtils.isEmpty(baseAttrInfo.getAttrValueList())) {
                for (BaseAttrValue baseAttrValue : baseAttrInfo.getAttrValueList()) {
                    QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper = new QueryWrapper<>();
                    baseAttrValueQueryWrapper.eq("value_name", baseAttrValue.getValueName());
                    baseAttrValueQueryWrapper.eq("attr_id", baseAttrInfo.getId());
                    BaseAttrValue baseAttrValue1 = baseAttrValueMapper.selectOne(baseAttrValueQueryWrapper);
                    if (null == baseAttrValue1) {
                        baseAttrValue.setAttrId(baseAttrInfo.getId());
                        baseAttrValueMapper.insert(baseAttrValue);
                    }
                }
            }
        } else {
            QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper1 = new QueryWrapper<>();
            baseAttrValueQueryWrapper1.eq("attr_id", baseAttrInfo1.getId());
            List<BaseAttrValue> baseAttrValueList = baseAttrValueMapper.selectList(baseAttrValueQueryWrapper1);
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            Map<Long, BaseAttrValue> dbMap = baseAttrValueList.stream()
                    .filter(item -> item.getId() != null)
                    .collect(Collectors.toMap(BaseAttrValue::getId, item -> item));

            Map<Long, BaseAttrValue> inputMap = attrValueList.stream()
                    .filter(item -> item.getId() != null)
                    .collect(Collectors.toMap(BaseAttrValue::getId, item -> item));
            // === 处理删除 ===
            for (BaseAttrValue dbValue : baseAttrValueList) {
                Long id = dbValue.getId();
                if (id != null && !inputMap.containsKey(id)) {
                    // 不在前端传回的数据中，说明被删除了，做逻辑删除
                    dbValue.setIsDeleted(1); // 假设字段名为 isDeleted
                    baseAttrValueMapper.deleteBaseAttrValue(dbValue); // 或者用 update wrapper
                }
            }
            // === 处理新增 ===
            for (BaseAttrValue inputValue : attrValueList) {
                if (inputValue.getId() == null || !dbMap.containsKey(inputValue.getId())) {
                    // 新增数据
                    inputValue.setAttrId(baseAttrInfo.getId()); // 关联属性ID
                    baseAttrValueMapper.insert(inputValue);
                }
            }
        }
    }

    @Override
    public List<BaseAttrValue> getAttrValueList(Long id) {
        QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper = new QueryWrapper<>();
        baseAttrValueQueryWrapper.eq("attr_id",id);
       return baseAttrValueMapper.selectList(baseAttrValueQueryWrapper);

    }
}
